package tnb.android;

import java.util.ArrayList;
import java.util.List;

import tnb.android.R.string;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity
{
	private static final int EDIT_CONTACT = 0;
	private static final int DELETE_CONTACT =1;
	private static final int EXIT = 2;
	
	ArrayList<TextView> tvs;
	
	//Account information (Should be loaded at startup?)
	private String userName = "k";
	private String password = "k";

	//Accelerometer variables
	private SensorManager sensorManager;
    private float accel;
    private float accelCurrent;
    private float accelLast; 
	
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

		setContentView(R.layout.demo_layout);

		//Accelerometer settings
	    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
	    sensorManager.registerListener(sensorListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
	    accel = 0.00f;
	    accelCurrent = SensorManager.GRAVITY_EARTH;
	    accelLast = SensorManager.GRAVITY_EARTH;
		
	    //Button listeners
		((Button)findViewById(R.id.button1)).setOnClickListener(onClickListener);
		((Button)findViewById(R.id.button2)).setOnClickListener(onClickListener);
		
		//Set title
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
		TextView title = (TextView) (findViewById(R.id.title_bar_title));
		title.setText(getString(string.main_activity_s_title));

		//Set players
//		LinearLayout layout = (LinearLayout) findViewById(R.id.main_activity_layout);
//		tvs = new ArrayList<TextView>();
//		String[] values = new String[] {"Jocke", "Alle", "Iro", "Peter", "Emma"};
//		for(String s : values) {
//			TextView tv = (TextView)getLayoutInflater().inflate(R.layout.player, null);
//			tv.setText(s);
//			tvs.add(tv);
//			layout.addView(tv);
//		}
		
		ClientConnectionUtil.createAccount(userName, password);
		updateTestTextField();
	}

	//Gets achievements from server and updates the text field
	private void updateTestTextField()
	{
		List<Byte> achievementField = ClientConnectionUtil.getAchievments(userName, password);
		EditText e = (EditText)findViewById(R.id.editText1);
		e.setText("" + achievementField.get(0) + ", " + achievementField.get(1) + ", " + achievementField.get(2) + ", " + achievementField.get(3) + ", " + achievementField.get(4));
	}
	
	public boolean onCreateOptionsMenu(Menu menu){

		menu.add(0,EDIT_CONTACT,0,"Edit Contact");

		menu.add(0,DELETE_CONTACT,0,"Delete Contact");

		menu.add(0,EXIT,0,"Exit");

		return true;

	}

	public boolean onOptionsItemSelected (MenuItem item){

		switch (item.getItemId()){

		case EDIT_CONTACT:
			Toast.makeText(this, "Edit Contact", Toast.LENGTH_SHORT).show();
			return true;

		case DELETE_CONTACT :
			Toast.makeText(this, "Delete Contact", Toast.LENGTH_SHORT).show();
			return true;
		}	
		return false;
	}

	OnClickListener onClickListener = new OnClickListener() 
	{
	    public void onClick(View v) 
	    {

	    	switch (v.getId()) 
			{
				case R.id.progress_button:
					Intent progressIntent = new Intent(MainActivity.this, ProgressActivity.class);
					startActivity(progressIntent);
					break;
				case R.id.button1:
					ClientConnectionUtil.earnAchievement((byte)1, userName, password);
					updateTestTextField();
					break;
				case R.id.button2:
					ClientConnectionUtil.earnAchievement((byte)2, userName, password);
					updateTestTextField();
					break;
			}
	    	
//			for(TextView tv : tvs) {
//			if(tv.equals(view)) {
//				Intent intent = new Intent(MainActivity.this, ProgressActivity.class);
//				intent.putExtra("player", tv.getText());
//				startActivity(intent);
//			}	    	
//		}
	    }
	};
	
	//Accelerometer listener
	private SensorEventListener sensorListener = new SensorEventListener() {

	    public void onSensorChanged(SensorEvent se) 
	    {
		    float x = se.values[0];
		    float y = se.values[1];
		    float z = se.values[2];
		    accelLast = accelCurrent;
		    accelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
		    float delta = accelCurrent - accelLast;
		    accel = accel * 0.9f + delta;
		    
		    if(accel > 10)
		    {
				ClientConnectionUtil.earnAchievement((byte)3, userName, password);
				updateTestTextField();
		    }
	    }

	    public void onAccuracyChanged(Sensor sensor, int accuracy) { }
	  };

	
	public void onDestroy()
	{
		super.onDestroy();
	}
}